﻿using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebPartPages;
using TennisSite.WebParts;

namespace TennisSite.FeatureReceivers
{
    public class TennisSiteFeatureReceiver : SPFeatureReceiver
    {
        public const string COUNTRIES_LIST_NAME = "Countries";
        public const string COUNTRIES_LIST_URL = "/Lists/Countries";
        public const string COUNTRY_LOOKUP_FIELD_NAME = "Country";

        public const string PLAYERS_LIST_NAME = "Players";
        public const string PLAYERS_LIST_URL = "/Lists/Players";

        private const string MAIN_VIEW_FILE_PATH = "Lists/Players/MainView.aspx";

        public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
        {

        }

        public override void FeatureInstalled(SPFeatureReceiverProperties properties)
        {

        }

        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            using (SPWeb web = properties.Feature.Parent as SPWeb)
            {
                AddCoutriesLookUpField(web);

                AddWebPartToPlayersView(web);
            }
        }

        private void AddWebPartToPlayersView(SPWeb web)
        {
            SPFile mainViewFile = web.GetFile(MAIN_VIEW_FILE_PATH);
            SPLimitedWebPartManager manager = mainViewFile.GetLimitedWebPartManager(PersonalizationScope.Shared);

            manager.AddWebPart(new PlayersWebPart(), "Main", 3);

            mainViewFile.Update();
            web.Update();

            manager.Web.Dispose();
            manager.Dispose();
        }

        private void AddCoutriesLookUpField(SPWeb web)
        {
            SPList playersList = web.GetListByName(PLAYERS_LIST_NAME);
            SPList countriesList = web.GetListByName(COUNTRIES_LIST_NAME);

            playersList.Fields.AddLookup(COUNTRY_LOOKUP_FIELD_NAME, countriesList.ID, true);

            SPFieldLookup countryLookUpInPlayers = new SPFieldLookup(playersList.Fields, COUNTRY_LOOKUP_FIELD_NAME);
            countryLookUpInPlayers.LookupField = countriesList.Fields[COUNTRY_LOOKUP_FIELD_NAME].InternalName;

            countryLookUpInPlayers.Update();

            SPView defaultPlayersView = playersList.DefaultView;

            if (!defaultPlayersView.ViewFields.Exists(COUNTRY_LOOKUP_FIELD_NAME))
            {
                defaultPlayersView.ViewFields.Add(countryLookUpInPlayers);
            }

            defaultPlayersView.Update();


            web.Update();
        }

        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {
            using (SPWeb web = properties.Feature.Parent as SPWeb)
            {
                SPList countriesList = web.GetListByName(COUNTRIES_LIST_NAME);
                if (countriesList != null)
                {
                    web.Lists.Delete(countriesList.ID);
                }

                SPList playersList = web.GetListByName(PLAYERS_LIST_NAME);
                if (playersList != null)
                {
                    web.Lists.Delete(playersList.ID);
                }

                web.Update();
            }
        }
    }
}